package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.api.ResultCode;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.InterceptRequest;
import com.ytjj.qmyx.admin.service.InterceptService;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.mall.model.OrdersInterceptNote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 订单拦截列表
 */

@RestController
@RequestMapping("/intercept")
public class OrderInterceptController {

    @Autowired
    private InterceptService interceptService;

    @Autowired
    private MemberService memberService;

    /**
     * 拦截管理
     */
    @PostMapping("/interceptOrderList")
    @OperLog(operModule = "订单拦截管理", operDesc = "订单拦截列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult interceptOrderList(@RequestBody InterceptRequest interceptRequest){
        return CommonResult.success(interceptService.getInterceptOrderList(interceptRequest));
    }
    /**
     * 修改状态
     */
    @GetMapping("/updateStatus")
    @OperLog(operModule = "订单拦截管理", operDesc = "编辑状态", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateStatus(@RequestParam Integer id ,@RequestParam Integer status){
        String InterceptorName = memberService.getAdmin().getName();
        Integer interceptorId = memberService.getAdmin().getId();
        int count = interceptService.updateStatus(id, InterceptorName, interceptorId, status);
        if (count > 0) {
            return CommonResult.success(ResultCode.SUCCESS);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     * 新增备注
     */
    @PostMapping("/insertNote")
    @OperLog(operModule = "订单拦截管理", operDesc = "新增备注", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertNote(@RequestBody OrdersInterceptNote ordersInterceptNote){
        String name = memberService.getAdmin().getName();
        ordersInterceptNote.setAdminName(name);
        int count = interceptService.insertNote(ordersInterceptNote);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     * 查询备注
     */
    @GetMapping("/selectNote")
    @OperLog(operModule = "订单拦截管理", operDesc = "查询备注", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectNote (@RequestParam Integer ordersInterceptId){
        List<OrdersInterceptNote> list = interceptService.selectNote(ordersInterceptId);
        return CommonResult.success(list);
    }

    @GetMapping("/delIntercept")
    @OperLog(operModule = "订单拦截管理", operDesc = "移除", operType = SysLogConstants.OPERATION_DELETE)
    public CommonResult delete(@RequestParam Integer id){
        int count = interceptService.delIntercept(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
